<?php
class Myttest extends CI_Controller {
	
	function __construct() {
		// load controller parent
		parent::__construct ();
		$this->load->model('MPTtree');
		$this->MPTtree->set_table('categories'); 
		$this->load->helper(array('form', 'url'));
	}
	
function index() {
		$root = $this->MPTtree->get_root();
		print_r($root);
		//Update node it specified lft as 1-st argument
		//$this->MPTtree->update_node(1,array('title' => 'Продукти'));
		//Append node
//		$this->MPTtree->append_node_last(1, array('title' => 'Плочки'));
//		$this->MPTtree->append_node_last(1, array('title' => 'Батерии'));
//		$this->MPTtree->append_node_last(12, array('title' => 'кухни'));
//		$this->MPTtree->append_node_last(12, array('title' => 'спални'));
//		$this->MPTtree->append_node_last(12, array('title' => 'мека мебел'));
//		$this->MPTtree->append_node_last(12, array('title' => 'трапезарии'));
//		$this->MPTtree->append_node_last(12, array('title' => 'всекидневни'));
//		$this->MPTtree->append_node_last(28, array('title' => 'всекидневни'));

//		$this->MPTtree->insert_node_before(2, array('title' => 'Versache'));
$this->MPTtree->insert_node_after(10, array('title' => 'Вани'));
        
//		$this->MPTtree->delete_node(4);
		

//		$this->MPTtree->delete_node(9);
//		$this->MPTtree->delete_node(11);
		print_r('</br>');
		$this->load->model('Mycommon');
		print_r($this->Mycommon->getCategoryAsSelect());
		print_r($this->MPTtree->display());
		print_r($this->_my_display());
		$data ['title'] = 'My first application created with Code Igniter';
		$data ['message'] = 'Hello world!';
		// load 'helloworld' view$this->MPTtree->
		$this->load->view ( 'helloworld', $data );
	}
	
	function _my_display($lft = 1){
		$node = $this->MPTtree->get_node($lft);
		$str = '';
		$right = array();
		$query = $this->db->query('SELECT '.$this->MPTtree->title_col.', '.$this->MPTtree->left_col.', '.
									$this->MPTtree->right_col.' FROM '.$this->MPTtree->tree_table.
			' WHERE '.$this->MPTtree->left_col.' BETWEEN '.$node[$this->MPTtree->left_col].
			' AND '.$node[$this->MPTtree->right_col].
			' ORDER BY '.$this->MPTtree->left_col.' ASC');
        $str .='<SELECT>';
		foreach($query->result_array() as $row){
			if(count($right) > 0){
				while($right[count($right)-1] < $row[$this->MPTtree->right_col]){
					array_pop($right);
				}
			}
			$str .= '<option value="'.$row[$this->MPTtree->left_col].'">';
			$str .= str_repeat('- ',count($right)) . $row[$this->MPTtree->title_col] . "<br />\n";
			$str .='</option>';
			$right[] = $row[$this->MPTtree->right_col];
		}
		$str .='</SELECT>';
		return $str;
	}
}
?>